<?php

namespace app\api\controller;

use app\BaseController;
use frame\lib\Db;
use frame\lib\VerfiyParam;

class Article extends Base
{
    // 全站文章 
    public function index()
    {
        $user_id = $this->user_id;
        $sql = "select a.id , username as author ,content,title,heat,comment,a.status ,a.create_time  from article as a,user as u 
         where a.uid = u.id 
         && a.user_id = $user_id &&
         a.status = '1'
         && a.delete_time = 0
         ;";
        $list = Db::query($sql);
        success($list);
    }
    public function list()
    {
        $uid = $this->uid;
        $sql = "select a.id , username as author ,title,content,heat,comment,a.status ,a.create_time 
         from article as a,user as u 
        where a.uid = u.id 
        && a.uid = '$uid'
        && a.delete_time = '0'
        ;";
        $list = Db::query($sql);
        success($list);
    }
    public function add()
    {
        $param = $_POST;
        // echo $a;
        $rule = ['title|标题' => 'require', 'content' => 'require'];
        VerfiyParam::init($param, $rule);
        $uid = $this->uid;
        $data = [
            'title' => $param['title'],
            'content' => preventXSS($param['content']),
            'status' => 0,
            'uid' => $this->uid,
            'user_id' => $this->user_id,
            'create_time' => getTime()
        ];
        request_limit('article', "uid = $uid", 60);
        $res = Db::name('article')->insert($data);
        if ($res) success();
        error();
    }
    public function edit()
    {
        $param = $_POST;
        $rule = ['id' => 'require|int'];
        VerfiyParam::init($param, $rule);
        $art = Db::name('article')->where(['id' => $param['id']])->find();
        if (!$art) error('文章不存在');
        $content =  preventXSS($param['content']);
        $data = [
            'title' => @$param['title'] ?? $art['title'],
            'content' => @$param['content'] ? $content : $art['content'],
            'status' => 0,
            'update_time' => getTime()
        ];
        $res = Db::name('article')->where(['id' => $param['id']])->update($data);
        if ($res) success();
        error();
    }
    public function read()
    {
        $id = @$_GET['id'];
        if (!$id) error('缺少参数id', 201);
        $data = Db::name('article')->field('id,title,content,uid,heat , comment ,status , create_time')->where(['id' => $id, 'user_id' => $this->user_id, 'delete_time' => 0])->find();
        if (!$data) error('文章不存在');
        $user = Db::name('user')->field('id,username')->where(['id' => $data['uid']])->find();
        $data['author'] = $user['username'];
        $data['role'] = $user['id'] == $this->user_id ? '管理员' : '普通用户';
        // 添加访问量
        if ($data['uid'] != $this->uid) {
            if ($data['status'] == 0) error('文章待审核');
            Db::name('article')->where(['id' => $id, 'user_id' => $this->user_id])->update(['heat' => ++$data['heat']]);
        }
        $sql = "select c.id as cid,u.id as uid, comment ,username,c.create_time from comment as c , user as u 
        where c.uid = u.id 
        && c.delete_time = 0
        && art_id = '$id' 
        ";
        $data['commentList'] = Db::query($sql);
        success($data);
    }
    public function del()
    {
        $param = $_GET;
        $rule = ['id' => 'require|int'];
        VerfiyParam::init($param, $rule);
        $art = Db::name('article')->where(['id' => $param['id'], 'delete_time' => 0])->find();
        if (!$art) error('文章不存在');
        $res = Db::name('article')->where(['id' => $param['id']])->delete();
        if ($res) success();
        error();
    }
}
